package com.tech.view.panel.panelframe;

import com.tech.event.CloseActionListener;
import com.tech.util.LogProcessUtil;
import com.tech.view.common.ColorUtil;
import com.tech.view.common.FontUtil;
import com.tech.view.common.ResourcesUtil;
import com.tech.view.component.button.RoundButton;
import com.tech.view.component.button.SimpleIconButton;
import com.tech.view.component.input.SearchField;
import com.tech.view.frame.MainFrame;
import com.tech.view.panel.GoodsSendPanel;
import com.tech.view.panel.HOTPanel;
import com.tech.view.panel.IndexPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.logging.Logger;

/**
 * 窗口顶部面板
 */
public class HeaderPanel extends JPanel {
	private final static Logger LOG = LogProcessUtil.getLogger(HeaderPanel.class.getName());
	private int width;
	private int height;
	private MainFrame frame;
	private JPanel leftPanel;
	private JPanel centerPanel;
	private JPanel rightPanel;
	private HOTPanel hotPanel;

	// logo
	private JLabel logoLabel;
	// 标题
	private JLabel titleLabel;
	private String title = "好读书";
	// 搜索框
	private SearchField searchField;
	// 发旧书
	// private IconButton sendBookButton;
	// private SimpleIconButton sendBookButton;
	private RoundButton sendBookButton;
	// 个人中心
	private SimpleIconButton myCenterButton;
	// 消息中心
	private SimpleIconButton messageCenterButton;
	// 购物车
	private SimpleIconButton carButton;
	// 菜单，可以进行一些设置
	private SimpleIconButton menuButton;
	// 最小化
	private SimpleIconButton minimizeButton;
	// 最大化
	private SimpleIconButton maximizeButton;
	// 关闭
	private SimpleIconButton closeButton;

	public HeaderPanel(MainFrame frame, int width, int height, HOTPanel hotPanel) {
		this.frame = frame;
		this.width = width;
		this.height = height;
		this.hotPanel = hotPanel;
		initService();
		initComponents();
		setListeners();
		initView();
	}

	private void initService() {
	}

	private void initComponents() {
		leftPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, (height - 32) / 2));
		centerPanel = new JPanel(new BorderLayout(0, 0));
		rightPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, (height - 32) / 2));

		logoLabel = new JLabel(new ImageIcon(ResourcesUtil.getImage(ResourcesUtil.Image.LOGO)));

		titleLabel = new JLabel(title);
		titleLabel.setFont(FontUtil.TITLE_FONT);

		// 搜索框
		searchField = new SearchField("请输入搜索关键词");
		searchField.addActionListener(e -> {
			hotPanel.searchGoods(searchField.getText());
		});

		// TODO 暂时拿图片替代
		sendBookButton = new RoundButton("发旧书", new ImageIcon(ResourcesUtil.getImage(ResourcesUtil.Image.SEND_GOODS_ICON)),
				ColorUtil.LIGHT_BLUE_COLOR, 9);
		sendBookButton.setForeground(Color.WHITE);
		sendBookButton.setFont(FontUtil.TITLE_FONT);
		sendBookButton.setMargin(new Insets(2, 8, 2, 8));

		myCenterButton = new SimpleIconButton(new ImageIcon(ResourcesUtil.getImage(ResourcesUtil.Image.USER_ICON)), ColorUtil.BACKGROUND_COLOR);

		messageCenterButton = new SimpleIconButton(new ImageIcon(ResourcesUtil.getImage(ResourcesUtil.Image.MESSAGE_ICON)), ColorUtil.BACKGROUND_COLOR);

		carButton = new SimpleIconButton(new ImageIcon(ResourcesUtil.getImage(ResourcesUtil.Image.CAR_ICON)), ColorUtil.BACKGROUND_COLOR);

		menuButton = new SimpleIconButton(new ImageIcon(ResourcesUtil.getImage(ResourcesUtil.Image.MENU_ICON)), ColorUtil.BACKGROUND_COLOR);

		minimizeButton = new SimpleIconButton(new ImageIcon(ResourcesUtil.getImage(ResourcesUtil.Image.MIN_ICON)), ColorUtil.BACKGROUND_COLOR);
		// 由于最小化的图标高度只有1，需要把按钮的预设大小调高
		Dimension preferredSize = minimizeButton.getPreferredSize();
		preferredSize.height = preferredSize.width + 1;
		minimizeButton.setPreferredSize(preferredSize);
		maximizeButton = new SimpleIconButton(new ImageIcon(ResourcesUtil.getImage(ResourcesUtil.Image.MAX_ICON)), ColorUtil.BACKGROUND_COLOR);

		closeButton = new SimpleIconButton(new ImageIcon(ResourcesUtil.getImage(ResourcesUtil.Image.CLOSE_ICON)), ColorUtil.BACKGROUND_COLOR);
	}

	private void initView() {
		//给父窗体初始化拖拽事件
		DragEvent.initDragEvent(frame, this);
		//设置高度
		setPreferredSize(new Dimension(width, height));
		setLayout(new BorderLayout(20, 15));


		leftPanel.add(logoLabel);
		leftPanel.add(titleLabel);

		// 搜索框的宽度自适应
		centerPanel.add(Box.createVerticalStrut((height - 32) / 2), BorderLayout.NORTH);
		centerPanel.add(searchField);
		centerPanel.add(Box.createVerticalStrut((height - 32) / 2), BorderLayout.SOUTH);

		rightPanel.add(sendBookButton);
		rightPanel.add(myCenterButton);
		rightPanel.add(messageCenterButton);
		rightPanel.add(carButton);
		rightPanel.add(menuButton);
		rightPanel.add(minimizeButton);
		rightPanel.add(maximizeButton);
		rightPanel.add(closeButton);

		add(leftPanel, BorderLayout.WEST);
		add(centerPanel);
		add(rightPanel, BorderLayout.EAST);
	}

	private void setListeners() {
		// 点击LOGO或标题回到首页
		logoLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.switchContentPanel(new IndexPanel(frame));
			}
		});
		titleLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.switchContentPanel(new IndexPanel(frame));
			}
		});

		//双击标题栏，进行缩放
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					reSize(frame);
				}
			}
		});
		sendBookButton.addActionListener(e -> {
			frame.switchContentPanel(new GoodsSendPanel(frame));
		});
		menuButton.addActionListener(e -> {

		});
		// 最小化
		minimizeButton.addActionListener(e -> frame.setExtendedState(JFrame.ICONIFIED));
		// 最大化
		maximizeButton.addActionListener(e -> reSize(frame));
		// 关闭
		closeButton.addActionListener(new CloseActionListener());
	}

	/**
	 * 根据状态进行缩放
	 *
	 * @param frame 目标窗体
	 */
	private void reSize(JFrame frame) {
		int state = frame.getExtendedState();

		if (state == JFrame.MAXIMIZED_BOTH) {
			//还原最大化之前的尺寸
			frame.setExtendedState(JFrame.NORMAL);
		} else if (state == JFrame.ICONIFIED || state == JFrame.NORMAL) {
			//最大化
			frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		}
	}
}

